#include <ESP8266WiFi.h>
#include "DHT.h"
#include <U8g2lib.h>
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ 14, /* data=*/ 2, /* reset=*/ U8X8_PIN_NONE);
unsigned char rx[8];
// WiFi parameters
//const char* ssid = "EST-WLAN";
//const char* password = "whutest123";
//
const char* ssid = "EST-WLAN";
const char* password = "whutest123";
#define DHTPIN 5

#define DHTTYPE DHT11

// Initialize DHT sensor
DHT dht(DHTPIN, DHTTYPE, 15);

const char* host = "dweet.io";

void setup() {
  u8g2.begin();
  Serial.begin(115200);
  delay(10);
  
  dht.begin();

  // Connecting to a WiFi network  (not working)
//  Serial.println();
//  Serial.println();
//  Serial.print("Connecting to ");
//  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
//    Serial.print(".");
  }

//  Serial.println("");
//  Serial.println("WiFi connected");  
//  Serial.println("IP address: ");
//  Serial.println(WiFi.localIP());
}
int incomedate = 0;
void loop() {

//  Serial.print("Connecting to ");
//  Serial.println(host);
  
  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
  //  Serial.println("connection failed");
    return;
  }
    
  // Reading temperature and humidity
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  
  while (isnan(h) || isnan(t)) {
 //   Serial.println("Failed to read from DHT sensor!");
    delay(1000);
    
    // Get the measurements once more
    h = dht.readHumidity(); 
    t = dht.readTemperature();
  }
  
//    Serial.println();
//    Serial.println("The temperature and humidity are:");
//    Serial.println(t);
//    Serial.println(h);
    delay(100);
  u8g2.clearBuffer();          // clear the internal memory
  u8g2.setFont(u8g2_font_inb16_mf); // choose a suitable font
  // Display data
  u8g2.setCursor(15, 23);    //OLED   128  X  64  
  u8g2.print("T:"); 
  u8g2.print(t); 
  u8g2.setCursor(15, 46);
  u8g2.print("H:"); 
  u8g2.print(h);  
  u8g2.setCursor(20, 63);
  u8g2.setFont(u8g2_font_ncenB08_tr);
  u8g2.print("IP:"); 
  u8g2.print(WiFi.localIP());
  u8g2.sendBuffer(); 
  if (Serial.available() > 0)
  {
      Serial.readBytes(rx,8);
//    if(rx[0]== 50)
//    {
//      if(rx[1]== 48)     //窗帘被拉上
//      {
//            client.print(String("GET /dweet/for/RGB_lamp?state=关") + " HTTP/1.1\r\n" +
//                 "Host: " + host + "\r\n" + 
//                 "Connection: close\r\n\r\n");
//      }
//      else if(rx[1]== 49)   //窗帘被打开
//      {
//              client.print(String("GET /dweet/for/status?state=开") + " HTTP/1.1\r\n" +
//                 "Host: " + host + "\r\n" + 
//                 "Connection: close\r\n\r\n");
//      }
//      if(rx[3]== 49)   //空气正常
//      {
//              client.print(String("GET /dweet/for/detect?air=0") + " HTTP/1.1\r\n" +
//                 "Host: " + host + "\r\n" + 
//                 "Connection: close\r\n\r\n");  
//      }
//      else if(rx[3]== 48)   //有害空气超标
//      {
//            client.print(String("GET /dweet/for/detect?air=1") + " HTTP/1.1\r\n" +
//                 "Host: " + host + "\r\n" + 
//                 "Connection: close\r\n\r\n");    
//      }
//    }
 
    }
    // Send the request to the server
    client.print(String("GET /dweet/for/DHTesp8266?temperature=") + String(t) + "&humidity=" + String(h) + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" + 
                 "Connection: close\r\n\r\n");
                                 
    client.print(String("GET /dweet/for/RGB_lamp?state=off") + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" + 
                 "Connection: close\r\n\r\n");
    client.print(String("GET /dweet/for/status?curtain=") + String("off") + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" + 
                 "Connection: close\r\n\r\n");  
//    if(rx[4]=='1'){
//                 client.print(String("GET /dweet/for/gate?status=") + String("succeed") + " HTTP/1.1\r\n" +
//                 "Host: " + host + "\r\n" + 
//                 "Connection: close\r\n\r\n"); 
//    }
//    if(rx[4]=='0'){
//                 client.print(String("GET /dweet/for/gate?status=") + String("failed") + " HTTP/1.1\r\n" +
//                 "Host: " + host + "\r\n" + 
//                 "Connection: close\r\n\r\n");
//    }
    client.print(String("GET /dweet/for/detect?air=") + String(rx[3]-48) + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" + 
                 "Connection: close\r\n\r\n");                         
    unsigned long timeout = millis();
    while (client.available() == 0) {
      if (millis() - timeout > 5000) {
        Serial.println(">>> Client Timeout !");
        client.stop();
        return;
    }
  }
  
    // Read all the lines of the reply from server and print them to Serial
    while(client.available()){
      String line = client.readStringUntil('\r');
 //     Serial.print(line);
    }
 //   Serial.println();
  //  Serial.println("closing connection");
  //  Serial.println();
  
  // Repeat every 0.1 seconds
  delay(100);
 }
